package artid.com.arttogoa.photo;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import artid.com.arttogoa.R;
import artid.com.arttogoa.parser.RecordDetailList;

/**
 * 图片查看器
 */
public class ImagePagerActivity extends FragmentActivity implements
OnClickListener {
	private static final String STATE_POSITION = "STATE_POSITION";
	public static final String EXTRA_IMAGE_INDEX = "image_index";
	public static final String EXTRA_IMAGE_URLS = "image_urls";
	public static final String EXTRA_IMAGE_TYPE = "image_type";
	/** 从相册进入 */
	public static final int ALBUM = 1;
	/** 从九宫格进入 */
	public static final int LATTICE = 2;
	/** 从备案详情 */
	public static final int MICSHOW = 3;
	private int type;
	private HackyViewPager mPager;
	private int pagerPosition;
	private TextView indicator, info;
	private ImageView back, edit;
	private ImagePagerAdapter mAdapter;
	private ArrayList<String> urls;
	private ArrayList<RecordDetailList.Pointinformation> micList;
	private RecordDetailList item;
	private int Is_phone;
	@SuppressWarnings("deprecation")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.image_detail_pager);
		indicator = (TextView) findViewById(R.id.title_text);
		type = getIntent().getIntExtra(EXTRA_IMAGE_TYPE, 0);
		info = (TextView) findViewById(R.id.tv_info);
		info.setVisibility(View.GONE);
		Is_phone = getIntent().getIntExtra("intentType", 0);
		initType();
		pagerPosition = getIntent().getIntExtra(EXTRA_IMAGE_INDEX, 0);

		//		type = getIntent().getIntExtra(EXTRA_IMAGE_TYPE, 0);
		//		indicator = (TextView) findViewById(R.id.title_text);
		//		initType();
		mPager = (HackyViewPager) findViewById(R.id.pager);
		mAdapter = new ImagePagerAdapter(getSupportFragmentManager(), urls);
		mPager.setAdapter(mAdapter);

		//		
		back = (ImageView) findViewById(R.id.title_back);
		edit = (ImageView) findViewById(R.id.title_menu);
		edit.setVisibility(View.GONE);
		back.setOnClickListener(this);
		edit.setOnClickListener(this);

		// CharSequence text = getString(R.string.viewpager_indicator, 1,
		// mPager.getAdapter().getCount());
		// indicator.setText(text); 
		// 更新下标
		mPager.setOffscreenPageLimit(0);
		mPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageSelected(int arg0) {
				// CharSequence text = getString(R.string.viewpager_indicator,
				// arg0 + 1, mPager.getAdapter().getCount());
				if (type == MICSHOW) {
					if (moreOne) {
						indicator.setText("第" + (arg0 / 2 + 1) + "个点的"
								+ (arg0 % 2 == 1 ? "200" : "60") + "倍微观图及描述信息");
						info.setText(micList.get(arg0 / 2).getText());
						info.setVisibility(View.VISIBLE);
					} else {
						indicator.setText("第" + (arg0 + 1) + "个点的微观图及描述信息");
						info.setText(micList.get(arg0).getText());
						info.setVisibility(View.VISIBLE);
					}
				} else {
					indicator.setText((arg0 + 1) + "/" + urls.size());
				}
			}

		});
		if (savedInstanceState != null) {
			pagerPosition = savedInstanceState.getInt(STATE_POSITION);
		}

		mPager.setCurrentItem(pagerPosition);
	}

	private boolean moreOne = false;

	/**
	 * 
	 */
	private void initType() {
		// TODO Auto-generated method stub
		switch (type) {
		case LATTICE:
			urls = getIntent().getStringArrayListExtra(EXTRA_IMAGE_URLS);
			indicator.setText((pagerPosition + 1) + "/" + urls.size());
			break;
		case ALBUM:
			urls = getIntent().getStringArrayListExtra(EXTRA_IMAGE_URLS);
			indicator.setText((pagerPosition + 1) + "/" + urls.size());
			break;
		case MICSHOW:
			item = (RecordDetailList) getIntent().getSerializableExtra(
					EXTRA_IMAGE_URLS);
			urls = new ArrayList<String>();
			ArrayList<RecordDetailList.Pointinformation> list = item.getPointinformation();
			micList = new ArrayList<RecordDetailList.Pointinformation>();
			for (int i = 3; i < list.size(); i++) {
				micList.add(list.get(i));
			}
			if(Is_phone==2){
				//如果为2手机备案
				for (int i = 0; i < micList.size(); i++) {
					moreOne = false;
					urls.add(micList.get(i).getImgurlone());
				}
			}else{
				for (int i = 0; i < micList.size(); i++) {
					if (TextUtils.isEmpty(micList.get(i).getImgUrlSixty())) {
						moreOne = false;
						urls.add(micList.get(i).getImgurl());
					} else {
						moreOne = true;
						urls.add(micList.get(i).getImgurl());
						urls.add(micList.get(i).getImgUrlSixty());
					}
				}
			}
			if (moreOne) {
				indicator.setText("第1个点的60倍微观图及描述信息");
				info.setText(micList.get(0).getText());
				info.setVisibility(View.VISIBLE);
			} else {
				indicator.setText("第1个点的微观图及描述信息");
				info.setText(micList.get(0).getText());
				info.setVisibility(View.VISIBLE);
			}
			break;

		default:
			urls = getIntent().getStringArrayListExtra(EXTRA_IMAGE_URLS);
			break;
		}
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		outState.putInt(STATE_POSITION, mPager.getCurrentItem());
	}

	private class ImagePagerAdapter extends FragmentStatePagerAdapter {

		public ArrayList<String> fileList;

		public ImagePagerAdapter(FragmentManager fm, ArrayList<String> fileList) {
			super(fm);
			this.fileList = fileList;
		}

		@Override
		public int getCount() {
			return fileList == null ? 0 : fileList.size();
		}

		@Override
		public Fragment getItem(int position) {
			String url = fileList.get(position);
			return ImageDetailFragment.newInstance(url);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.title_back:
			finish();
			break;
		case R.id.title_menu:

			break;

		default:
			break;
		}
	}
}
